//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package javax.vpncrypto;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.Provider.Service;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Iterator;
import java.util.List;
import sun.security.jca.GetInstance;
import sun.security.jca.GetInstance.Instance;

public class SecretKeyFactory {
    private Provider provider;
    private final String algorithm;
    private volatile javax.vpncrypto.SecretKeyFactorySpi spi;
    private final Object lock = new Object();
    private Iterator<Service> serviceIterator;

    protected SecretKeyFactory(javax.vpncrypto.SecretKeyFactorySpi var1, Provider var2, String var3) {
        this.spi = var1;
        this.provider = var2;
        this.algorithm = var3;
    }

    private SecretKeyFactory(String var1) throws NoSuchAlgorithmException {
        this.algorithm = var1;
        List var2 = GetInstance.getServices("SecretKeyFactory", var1);
        this.serviceIterator = var2.iterator();
        if (this.nextSpi((javax.vpncrypto.SecretKeyFactorySpi)null) == null) {
            throw new NoSuchAlgorithmException(var1 + " SecretKeyFactory not available");
        }
    }

    public static final SecretKeyFactory getInstance(String var0) throws NoSuchAlgorithmException {
        return new SecretKeyFactory(var0);
    }

    public static final SecretKeyFactory getInstance(String var0, String var1) throws NoSuchAlgorithmException, NoSuchProviderException {
        Instance var2 = javax.vpncrypto.JceSecurity.getInstance("SecretKeyFactory", javax.vpncrypto.SecretKeyFactorySpi.class, var0, var1);
        return new SecretKeyFactory((javax.vpncrypto.SecretKeyFactorySpi)var2.impl, var2.provider, var0);
    }

    public static final SecretKeyFactory getInstance(String var0, Provider var1) throws NoSuchAlgorithmException {
        Instance var2 = javax.vpncrypto.JceSecurity.getInstance("SecretKeyFactory", javax.vpncrypto.SecretKeyFactorySpi.class, var0, var1);
        return new SecretKeyFactory((javax.vpncrypto.SecretKeyFactorySpi)var2.impl, var2.provider, var0);
    }

    public final Provider getProvider() {
        synchronized(this.lock) {
            this.serviceIterator = null;
            return this.provider;
        }
    }

    public final String getAlgorithm() {
        return this.algorithm;
    }

    private javax.vpncrypto.SecretKeyFactorySpi nextSpi(javax.vpncrypto.SecretKeyFactorySpi var1) {
        synchronized(this.lock) {
            if (var1 != null && var1 != this.spi) {
                return this.spi;
            } else if (this.serviceIterator == null) {
                return null;
            } else {
                while(true) {
                    Service var3;
                    do {
                        if (!this.serviceIterator.hasNext()) {
                            this.serviceIterator = null;
                            return null;
                        }

                        var3 = (Service)this.serviceIterator.next();
                    } while(!javax.vpncrypto.JceSecurity.canUseProvider(var3.getProvider()));

                    try {
                        Object var4 = var3.newInstance((Object)null);
                        if (var4 instanceof javax.vpncrypto.SecretKeyFactorySpi) {
                            javax.vpncrypto.SecretKeyFactorySpi var5 = (javax.vpncrypto.SecretKeyFactorySpi)var4;
                            this.provider = var3.getProvider();
                            this.spi = var5;
                            javax.vpncrypto.SecretKeyFactorySpi var10000 = var5;
                            return var10000;
                        }
                    } catch (NoSuchAlgorithmException var7) {
                    }
                }
            }
        }
    }

    public final javax.vpncrypto.SecretKey generateSecret(KeySpec var1) throws InvalidKeySpecException {
        if (this.serviceIterator == null) {
            return this.spi.engineGenerateSecret(var1);
        } else {
            Exception var2 = null;
            javax.vpncrypto.SecretKeyFactorySpi var3 = this.spi;

            while(true) {
                try {
                    return var3.engineGenerateSecret(var1);
                } catch (Exception var5) {
                    if (var2 == null) {
                        var2 = var5;
                    }

                    var3 = this.nextSpi(var3);
                    if (var3 == null) {
                        if (var2 instanceof InvalidKeySpecException) {
                            throw (InvalidKeySpecException)var2;
                        }

                        throw new InvalidKeySpecException("Could not generate secret key", var2);
                    }
                }
            }
        }
    }

    public final KeySpec getKeySpec(javax.vpncrypto.SecretKey var1, Class<?> var2) throws InvalidKeySpecException {
        if (this.serviceIterator == null) {
            return this.spi.engineGetKeySpec(var1, var2);
        } else {
            Exception var3 = null;
            javax.vpncrypto.SecretKeyFactorySpi var4 = this.spi;

            while(true) {
                try {
                    return var4.engineGetKeySpec(var1, var2);
                } catch (Exception var6) {
                    if (var3 == null) {
                        var3 = var6;
                    }

                    var4 = this.nextSpi(var4);
                    if (var4 == null) {
                        if (var3 instanceof InvalidKeySpecException) {
                            throw (InvalidKeySpecException)var3;
                        }

                        throw new InvalidKeySpecException("Could not get key spec", var3);
                    }
                }
            }
        }
    }

    public final javax.vpncrypto.SecretKey translateKey(SecretKey var1) throws InvalidKeyException {
        if (this.serviceIterator == null) {
            return this.spi.engineTranslateKey(var1);
        } else {
            Exception var2 = null;
            SecretKeyFactorySpi var3 = this.spi;

            while(true) {
                try {
                    return var3.engineTranslateKey(var1);
                } catch (Exception var5) {
                    if (var2 == null) {
                        var2 = var5;
                    }

                    var3 = this.nextSpi(var3);
                    if (var3 == null) {
                        if (var2 instanceof InvalidKeyException) {
                            throw (InvalidKeyException)var2;
                        }

                        throw new InvalidKeyException("Could not translate key", var2);
                    }
                }
            }
        }
    }
}
